package org.gwt.grin.rebind;

import java.io.IOException;

/**
 * Used by the parser when it encounters something that might be a forward
 * reference. It's used to defer some computation in parsing until the reference
 * is resolved.
 * 
 * @author Bill Foote (http://jovial.com)
 */
public abstract class ForwardReference {

	private int lineNumber;
	private String fileName;
	private Lexer lexer;

	public ForwardReference(Lexer lexer) {
		this.lineNumber = lexer.getRealLineNumber();
		this.fileName = lexer.getRealFileName();
		this.lexer = lexer;
	}

	void resolveAtLine() throws IOException {
		int n = lexer.getLineNumber();
		String s = lexer.getFileName();
		lexer.setLineNumberAndName(lineNumber, fileName);
		resolve();
		lexer.setLineNumberAndName(n, s);
	}

	/**
	 * Called by the parser after the entire file has been parsed, and it's time
	 * to resolve this forward reference.
	 **/
	public abstract void resolve() throws IOException;

	/**
	 * Convenience method for reporting an error. The error message gives the
	 * line number where the construct we represent was read.
	 **/
	public void reportError(String msg) throws IOException {
		throw new IOException(msg + " on line " + lineNumber + " of "
				+ fileName + "!");
	}
}
